package com.plants.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.plants.back.pojo.PlantsGoods;
import com.plants.back.pojo.PlantsOrders;
import com.plants.back.pojo.PlantsShop;
import com.plants.back.pojo.Vo;
import com.plants.back.service.impl.PlantsGoodsServiceImpl;
import com.plants.back.service.impl.PlantsShopServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车控制器
 * </p>
 *
 * @author plants
 * @since 2024-08-06
 */
@CrossOrigin
@RestController
@RequestMapping("/plants_shop")
public class PlantsShopController {

    @Resource
    private PlantsShopServiceImpl shopService;

    @Resource
    private PlantsGoodsServiceImpl goodsService;

    // 新增购物车数据
    @PostMapping("/insert")
    public Vo<PlantsShop> insert(@RequestBody PlantsShop shop) {
        // 先查询出来是否有相同的
        LambdaQueryWrapper<PlantsShop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlantsShop::getGoodsName,shop.getGoodsName());
        wrapper.eq(PlantsShop::getUId,shop.getUId());
        PlantsShop one = shopService.getOne(wrapper);
        if (one != null){
            // 说明已经添加过，增加数量即可
            shop.setCount(one.getCount()+1);
            LambdaQueryWrapper<PlantsGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(PlantsGoods::getId,shop.getGId());
            PlantsGoods goods = goodsService.getOne(lambdaQueryWrapper);
            shop.setId(one.getId());
            boolean b = shopService.updateById(shop);
            if (b) {
                // 新增成功
                return new Vo<>(b, "添加成功！");
            } else {
                // 新增失败
                return new Vo<>(b, "添加失败，请稍后重试！");
            }
        }
        boolean b = shopService.save(shop);
        if (b) {
            // 新增成功
            return new Vo<>(b, "添加成功！");
        } else {
            // 新增失败
            return new Vo<>(b, "添加失败，请稍后重试！");
        }
    }
    // 修改购物车商品数量
    @PostMapping("/update")
    public Vo<PlantsShop> update(@RequestBody PlantsShop shop) {
        boolean b = shopService.updateById(shop);
        if (b){
            return new Vo<>(b,"");
        }else {
            return new Vo<>(b,"");
        }
    }

    // 删除购物车数据
    @GetMapping("/delete/{id}")
    public Vo<PlantsShop> delete(@PathVariable("id") String id) {
        UpdateWrapper<PlantsShop> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        boolean b = shopService.remove(wrapper);
        if (b) {
            return new Vo<>(b, "删除成功！");
        } else {
            return new Vo<>(b, "删除失败，请稍后重试！");
        }
    }

    // 按照用户id查询购物车数据
    @GetMapping("/shop_user/{uid}")
    public Vo<PlantsShop> shopUser(@PathVariable("uid") String uid) {
        LambdaQueryWrapper<PlantsShop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PlantsShop::getUId,uid);
        List<PlantsShop> list = shopService.list(wrapper);
        if (list.size() <= 0){
            // 没有数据
            return new Vo<>(false,"目前购物车中没有数据");
        }else {
            return new Vo<>(true,list);
        }
    }


}
